package com.hospital.frontdesk.fronxiang.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.hospital.frontdesk.fronxiang.entity.FrontdeskCard;
import com.hospital.frontdesk.fronxiang.service.FrontdeskCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @description：用户业务
 * @author：xyj
 * @date：2021/9/16 14:30
 * @versions：v1.0
 */
@Controller
@RequestMapping("/front")
public class FrontdeskCardController {

    @Autowired
    private FrontdeskCardService frontdeskCardService;

    @RequestMapping("/toRegister")
    public String home() {
        return "/frontdesk/register";
    }

    /**
     * 注册
     *
     * @param pwd
     * @param idCard
     * @param name
     * @param sex
     * @param phone
     * @param age
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public String register(String pwd, String idCard, String name, String sex, String phone, Integer age) {
        boolean register = frontdeskCardService.register(new FrontdeskCard(pwd, idCard, name, sex, phone, age));
        if (register) {
            FrontdeskCard id = frontdeskCardService.id(idCard);
            return JSON.toJSONString(id.getId());
        }
        return JSON.toJSONString(register);
    }

    @RequestMapping("/pagecard")
    public String pagefrontdeskcard(Model model, @RequestParam(defaultValue = "-1") Integer id, @RequestParam(value = "curr", defaultValue = "1") Integer curr, @RequestParam(defaultValue = "5") Integer pageSize) {
        List<FrontdeskCard> pagefrontdeskcard = frontdeskCardService.pagefrontdeskcard(id, curr, pageSize);
        PageInfo<FrontdeskCard> page = new PageInfo<>(pagefrontdeskcard);
        model.addAttribute("page", page);
        model.addAttribute("id", id);
        return "/frontdesk/reissue";
    }

    @RequestMapping("/recharging")
    @ResponseBody
    public String recharging(Integer id, Double mount) {
        boolean recharging = frontdeskCardService.recharging(id, mount);
        return JSON.toJSONString(recharging);
    }

    @RequestMapping("/iduser")
    @ResponseBody
    public String iduser(String idCard) {
        FrontdeskCard id = frontdeskCardService.id(idCard);
        return JSON.toJSONString(id);
    }

    @RequestMapping("/reissue")
    @ResponseBody
    public String reissue(Integer ids, String pwd, String idCard, String name, String sex, String phone, Integer age) {
        FrontdeskCard id = frontdeskCardService.id(idCard);
        if (id.getPassword().equals(pwd)) {
            boolean register = frontdeskCardService.register(new FrontdeskCard(pwd, idCard, name, sex, phone, age, id.getMoney()));
            if (register) {
                boolean update = frontdeskCardService.frontdeskcardupdate(ids);
                return JSON.toJSONString(register);
            } else {
                return JSON.toJSONString(false);
            }

        } else {
            return JSON.toJSONString(false);
        }


    }
}
